package com.mzpai.api.controller;
import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.gexin.fastjson.JSONObject;
import com.mzpai.product.exception.ProductResExceptionCode;
import com.mzpai.product.model.StoreUserSeckillCollect;
import com.mzpai.product.service.StoreUserSeckillCollectService;
import com.mzpai.product.vo.StoreUserSeckillCollectReqVo;
import com.mzpai.util.DateUtil;
import com.mzpai.vo.ResponseVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
@Api(value = "门店秒杀商品用户想要收集表")
@RestController
@RequestMapping("/storeUserSeckillCollect")
public class StoreUserSeckillCollectController extends BaseController {

	
	private static Logger log = LoggerFactory.getLogger(StoreUserSeckillCollectController.class);
	
	@Autowired 
	private StoreUserSeckillCollectService storeUserSeckillCollectService;
	
	
	@ApiOperation(value = "想要秒杀商品")
    @RequestMapping(value = "/wantSeckillProduct")
    public ResponseVo<Boolean> wantSeckillProduct(@RequestParam(value = "storeUserSeckillCollectStr", required = true) String storeUserSeckillCollectStr){
		
		log.info("msg1=开始想要秒杀商品,,method=wantSeckillProduct,,storeUserSeckillCollectStr={}", storeUserSeckillCollectStr);
		ResponseVo<Boolean> resVo=new ResponseVo<Boolean>();
		StoreUserSeckillCollectReqVo reqVo = JSONObject.parseObject(storeUserSeckillCollectStr, StoreUserSeckillCollectReqVo.class);
		
		StoreUserSeckillCollect storeUserSeckillCollect=buildStoreUserSeckillCollect(reqVo);
		
		Integer storeId=storeUserSeckillCollect.getStoreId();
		Integer productId=storeUserSeckillCollect.getProductId();
		Integer productAttrvalueId=storeUserSeckillCollect.getProductAttrvalueId();
		Integer userId=storeUserSeckillCollect.getUserId();
		Integer seckillId=storeUserSeckillCollect.getSeckillId();
		
		if(storeId==null){
			ResponseVo<Boolean> resParamVo=new ResponseVo<Boolean>();
			resParamVo.setCode(ProductResExceptionCode.STOREID_NULL.getCode());
			resParamVo.setMessage(ProductResExceptionCode.STOREID_NULL.getMessage());
			return resParamVo;
		}
		if(productId==null){
			ResponseVo<Boolean> resParamVo=new ResponseVo<Boolean>();
			resParamVo.setCode(ProductResExceptionCode.PRODUCTCODE_LIST_NULL.getCode());
			resParamVo.setMessage(ProductResExceptionCode.PRODUCTCODE_LIST_NULL.getMessage());
			return resParamVo;
		}
		if(productAttrvalueId==null){
			ResponseVo<Boolean> resParamVo=new ResponseVo<Boolean>();
			resParamVo.setCode(ProductResExceptionCode.PRODUCT_ATTRVALUE_ID_NULL.getCode());
			resParamVo.setMessage(ProductResExceptionCode.PRODUCT_ATTRVALUE_ID_NULL.getMessage());
			return resParamVo;
		}
		if(userId==null){
			ResponseVo<Boolean> resParamVo=new ResponseVo<Boolean>();
			resParamVo.setCode(ProductResExceptionCode.USER_IDNULL_ERROR.getCode());
			resParamVo.setMessage(ProductResExceptionCode.USER_IDNULL_ERROR.getMessage());
			return resParamVo;
		}
		//校验是否点过想要
		Map<String,Object> queryMap=new HashMap<String,Object>();
		queryMap.put("storeId", storeId);
		queryMap.put("userId", userId);
		queryMap.put("seckillId", seckillId);
		List<StoreUserSeckillCollect> list=storeUserSeckillCollectService.getList(queryMap);
		
		if(list==null || list.isEmpty()){
			try{
				storeUserSeckillCollectService.save(storeUserSeckillCollect);
				resVo.setResult(true);
				resVo.setCode(200);
				resVo.setMessage("OK");
			} catch(Exception e) {
				logger.error("msg1= 保存想要秒杀商品信息异常， {}", e);
				resVo.setCode(-1);
				resVo.setMessage("操作失败!");
				resVo.setResult(false);
			}
		}else{
			resVo.setCode(ProductResExceptionCode.ALREADY_WANT.getCode());
			resVo.setMessage(ProductResExceptionCode.ALREADY_WANT.getMessage());
			resVo.setResult(false);
		}
    	return resVo;
	}
	
	
	private StoreUserSeckillCollect buildStoreUserSeckillCollect(StoreUserSeckillCollectReqVo reqVo){
		
		StoreUserSeckillCollect storeUserSeckillCollect=null;
		if(reqVo!=null){
			storeUserSeckillCollect=new StoreUserSeckillCollect();
			
			Integer storeId=reqVo.getStoreId();
			Integer seckillId=reqVo.getSeckillId();
			String startTimeStr=reqVo.getStartTime();
			String stopTimeStr=reqVo.getStopTime();
			Integer userId=reqVo.getUserId();
			Integer productId=reqVo.getProductId();
			Integer productAttrvalueId=reqVo.getProductAttrvalueId();
			String productName=reqVo.getProductName();
			
			storeUserSeckillCollect.setStoreId(storeId);
			storeUserSeckillCollect.setSeckillId(seckillId);
			Date startTime=null;
			Date stopTime=null;
			try {
				startTime = DateUtil.parseByPattern(startTimeStr, DateUtil.DATE_HH_MM_SS);
				stopTime=DateUtil.parseByPattern(stopTimeStr, DateUtil.DATE_HH_MM_SS);
			} catch (ParseException e) {
				e.printStackTrace();
			}
			storeUserSeckillCollect.setStartTime(startTime);
			storeUserSeckillCollect.setStopTime(stopTime);
			storeUserSeckillCollect.setUserId(userId);
			storeUserSeckillCollect.setProductId(productId);
			storeUserSeckillCollect.setProductAttrvalueId(productAttrvalueId);
			storeUserSeckillCollect.setProductName(productName);
			storeUserSeckillCollect.setAddTime(new Date());
			storeUserSeckillCollect.setUpdateTime(new Date());
		}
		return storeUserSeckillCollect;
	}
	
	
	
	
}